local skynet = require "skynet"
local tabletool = require "tabletool"
local filelog = require "filelog"
local timetool = require "timetool"
local json = require "cjson"
json.encode_sparse_array(true,1,1)
require "pubenum"
require "sssenum"

local tablereplaywrite = {
    server = nil,
}

function tablereplaywrite.init(service)
	if tablereplaywrite.server == nil then
		tablereplaywrite.server = service
	end
end

function tablereplaywrite.write_replay_head()
	local server = tablereplaywrite.server
    local tablelogic = server:get_logicbyname("tablelogic")
	local seatlogic = server:get_logicbyname("seatlogic")
	local table_data = server.table_data
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then return end
	local ridstring = ""
	local tipinfos = {}
	local tip = tablelogic.get_tip()
	for index,seat in ipairs(table_data.seats) do
		if table_data.conf.table_type == SSSTableType.TABLE_TYPE_FANGKA then tip = 0 end
		if seatlogic.is_ingame(seat) then
			if ridstring == "" then
				ridstring = ridstring .. tostring(seat.rid)
			else
				ridstring = ridstring .. "_" .. tostring(seat.rid)
			end
			table.insert(tipinfos, { rid = seat.rid, tip = tip, diamond = seat.diamond })
		end
    end
    local table_type_string = ""
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_ZUANSI then
        if table_data.conf.match_type == 0 then
            table_type_string = table_type_string .. tostring(EGameRoomType.ROOM_TYPE_ZIJIAN_ZUANSI)
        else
            table_type_string = table_type_string .. tostring(EGameRoomType.ROOM_TYPE_MATCH_ZUANSHI)
        end
    elseif table_data.conf.table_type == SSSTableType.TABLE_TYPE_ZJZUANSI then
        table_type_string = table_type_string .. tostring(EGameRoomType.ROOM_TYPE_ZIJIAN_ZUANSI)
    elseif table_data.conf.table_type == SSSTableType.TABLE_TYPE_FANGKA then
        table_type_string = table_type_string .. tostring(EGameRoomType.ROOM_TYPE_ZIJIAN_FANGKA)
    elseif table_data.conf.table_type == SSSTableType.TABLE_TYPE_COIN then
        if table_data.conf.match_type == 0 then
            table_type_string = table_type_string .. tostring(EGameRoomType.ROOM_TYPE_ZIJIAN_COIN)
        else
            table_type_string = table_type_string .. tostring(EGameRoomType.ROOM_TYPE_MATCH_COIN)
        end
    else
        table_type_string = table_type_string .. tostring(EGameRoomType.ROOM_TYPE_MATCH_COIN)
    end
	local now_timestring = os.date("%Y%m%d_%H%M%S")
	local logname = tostring(table_data.conf.uuid) .. "_" ..tostring(table_data.one_start_time) .. "_" 
					.. tostring(0) .. "_" .. ridstring .. "_" .. now_timestring .. "_" .. tostring(table_type_string)
	table_data.replaylogname = logname
	local logstr = ''
		.. table_data.conf.uuid .. '\n'
		.. table_data.conf.table_type .. '\n'
		.. json.encode(tipinfos) .. '\n'
		.. json.encode(table_data.conf) .. '\n'
		.. table_data.one_start_time
	skynet.call(".tablereplaylog", "lua", "write", logname, logstr)
end

function tablereplaywrite.write_cards_stack()
    local server = tablereplaywrite.server
    local table_data = server.table_data
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then return end
    local logname = table_data.replaylogname
    local cardstring = os.date("%Y%m%d_%H%M%S") .. "_" .. json.encode(table_data.cards_stack)
    if logname ~= "" then
        skynet.call(".tablereplaylog", "lua", "write", logname, cardstring)
    end
end

function tablereplaywrite.write_player_handcards()
    local server = tablereplaywrite.server
    local table_data = server.table_data
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then return end
    local seatlogic = server:get_logicbyname("seatlogic")
    local logname = table_data.replaylogname
    local handsinfos = {
        action_type = SSSActionType.ACTION_FAPAI,
        action_time = timetool.get_time(),
        cardsinfos = {},
    }
    for index,seat in ipairs(table_data.seats) do
        if  seatlogic.is_ingame(seat) then
            local cardsobj = { rid = seat.rid, cards = tabletool.deepcopy(seat.cards)}
            table.insert(handsinfos.cardsinfos, cardsobj)
        end
    end
    local cards_string = os.date("%Y%m%d_%H%M%S") .. "_" .. json.encode(handsinfos)
    if logname ~= "" then
        skynet.call(".tablereplaylog", "lua", "write", logname, cards_string)
    end 
end

function tablereplaywrite.write_banker_info()
    local server = tablereplaywrite.server
    local table_data = server.table_data
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then return end
    local bank_seat = table_data.seats[table_data.banker_seat_index]
    local bankerinfo = {
        rid = bank_seat.rid,
        action_type = DNActionType.ACTION_SET_BANKER,
        banker_index = table_data.banker_seat_index,
        banker_times = bank_seat.qiangzhuang_times,
    }
    local logname = table_data.replaylogname
    local logstr = os.date("%Y%m%d_%H%M%S") .. "_" .. json.encode(bankerinfo)
    if logname ~= "" then
        skynet.call(".tablereplaylog", "lua", "write", logname, logstr)
    end

end


function tablereplaywrite.write_action_info(seat, action_type, times, is_outoftime, diamond, coin)
    local server = tablereplaywrite.server
    local table_data = server.table_data
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then return end
    if seat ~= nil then
        if table_data.conf.table_type == SSSTableType.TABLE_TYPE_COIN then
            if is_outoftime == false and seat.coin ~= coin and coin ~= nil and coin ~= 0 then
                seat.coin = coin
            end
        elseif table_data.conf.table_type == SSSTableType.TABLE_TYPE_ZUANSI or table_data.conf.table_type == SSSTableType.TABLE_TYPE_ZJZUANSI then
            if is_outoftime == false and seat.diamond ~= diamond and diamond ~= nil and diamond ~= 0 then
                ---同步玩家在大厅服的钻石
                seat.diamond = diamond
            end
        end
        local actioninfos = {
            rid = seat.rid,
            action_type = action_type,
            action_time = timetool.get_time(),
            times = times,
            is_outoftime = false,
            diamond = diamond,
            coin = coin,
        }
        if is_outoftime == true then
            actioninfos.is_outoftime = true
        end
        local logname = table_data.replaylogname
        local logstr = os.date("%Y%m%d_%H%M%S") .. "_" .. json.encode(actioninfos)
        if logname ~= "" then
            skynet.call(".tablereplaylog", "lua", "write", logname, logstr)
        end
    end
end

function tablereplaywrite.write_balance_info()
    local server = tablereplaywrite.server
    local table_data = server.table_data
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then return end
    local records = table_data.tablerecords
    local seatlogic = server:get_logicbyname("seatlogic")
    local zhuangseat = table_data.seats[table_data.banker_seat_index]
    if not zhuangseat then return end
    local balanceinfo = {
        banker_index = table_data.banker_seat_index,
        banker_times = zhuangseat.qiangzhuang_times,
        score_diamond = table_data.conf.score_diamond,
        action_type = SSSActionType.ACTION_BALANCE,
        action_time = timetool.get_time(),
        playerinfos = {},
    }
    local banrecord = records.tmpdiamond
    for index, seat in ipairs(table_data.seats) do
        if  seatlogic.is_ingame(seat) then
            if banrecord[seat.rid] and records.addinfos[seat.rid] then
                local info = {
                    rid = seat.rid,
                    index = seat.index,
                    rolename = seat.playerinfo.rolename,
                    card_type = seat.card_type,
                    cards = seat.cards,
                    xiazhu_times = seat.xiazhu_times,
                    balance = banrecord[seat.rid],
                    addinfo = records.addinfos[seat.rid],
                }
                table.insert(balanceinfo.playerinfos, info)
            end
        end
    end
    local bastring = os.date("%Y%m%d_%H%M%S") .. "_" .. json.encode(balanceinfo) .. "\n" .. "end"
    local logname = table_data.replaylogname
    if logname ~= "" then
        skynet.call(".tablereplaylog", "lua", "write", logname, bastring)
    end
end

function tablereplaywrite.write_replay_log(logstr)
	local server = tablereplaywrite.server
	local table_data = server.table_data
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then return end
	local logname = table_data.replaylogname
    logstr = os.date("%Y%m%d_%H%M%S") .. "_" .. logstr
	if logname ~= "" then
		skynet.call(".tablereplaylog", "lua", "write", logname, logstr)
	end
end

return tablereplaywrite